<template>
    <div class="order-confirm">
        <!-- 头部 -->
        <div class="confirmOrder-header">
            <div class="header-content">
                <p>
                    <i class="el-icon-s-order"></i>
                </p>
                <p>确认订单</p>
            </div>
        </div>
        <!-- 头部END -->

        <!-- 用svg来替换图片的请求，性能优化 -->
        <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="position: absolute; width: 0px; height: 0px; overflow: hidden;">
            <defs>
                <symbol id="icon-add" viewBox="0 0 31 32">
                    <title>add</title>
                    <path d="M30.745 15.152h-14.382v-14.596c0-0.308-0.243-0.557-0.543-0.557s-0.543 0.249-0.543 0.557v14.596h-14.665c-0.3 0-0.543 0.249-0.543 0.557s0.243 0.557 0.543 0.557h14.665v15.177c0 0.307 0.243 0.557 0.543 0.557s0.543-0.249 0.543-0.557v-15.177h14.382c0.3 0 0.543-0.249 0.543-0.557s-0.243-0.557-0.543-0.557z" class="path1"></path>
                </symbol>
                <symbol id="icon-edit" viewBox="0 0 32 32">
                    <title>edit</title>
                    <path d="M28.287 8.51l-4.805-4.806 0.831-0.831c0.472-0.472 1.086-0.777 1.564-0.777 0.248 0 0.452 0.082 0.622 0.253l3.143 3.144c0.539 0.54 0.133 1.529-0.524 2.186l-0.831 0.831zM26.805 9.992l-1.138 1.138-4.805-4.806 1.138-1.138 4.805 4.806zM24.186 12.612l-14.758 14.762-4.805-4.806 14.758-14.762 4.805 4.806zM7.379 28.288l-4.892 1.224 1.223-4.894 3.669 3.67zM31.123 4.011l-3.143-3.144c-0.567-0.567-1.294-0.867-2.103-0.867-1.036 0-2.174 0.52-3.045 1.391l-20.429 20.436c-0.135 0.134-0.23 0.302-0.276 0.487l-2.095 8.385c-0.089 0.355 0.017 0.736 0.276 0.995 0.198 0.198 0.461 0.307 0.741 0.307 0.085 0 0.171-0.010 0.254-0.031l8.381-2.096c0.185-0.047 0.354-0.142 0.487-0.276l20.43-20.436c1.409-1.41 2.042-3.632 0.524-5.15v0z" class="path1"></path>
                </symbol>
                <symbol id="icon-del" viewBox="0 0 32 32">
                    <title>delete</title>
                    <path d="M11.355 4.129v-2.065h9.29v2.065h-9.29zM6.194 29.935v-23.742h19.613v23.742h-19.613zM30.968 4.129h-8.258v-3.097c0-0.569-0.463-1.032-1.032-1.032h-11.355c-0.569 0-1.032 0.463-1.032 1.032v3.097h-8.258c-0.569 0-1.032 0.463-1.032 1.032s0.463 1.032 1.032 1.032h3.097v24.774c0 0.569 0.463 1.032 1.032 1.032h21.677c0.569 0 1.032-0.463 1.032-1.032v-24.774h3.097c0.569 0 1.032-0.463 1.032-1.032s-0.463-1.032-1.032-1.032v0z" class="path1"></path>
                    <path d="M10.323 9.806c-0.569 0-1.032 0.463-1.032 1.032v14.452c0 0.569 0.463 1.032 1.032 1.032s1.032-0.463 1.032-1.032v-14.452c0-0.569-0.463-1.032-1.032-1.032z" class="path2"></path>
                    <path d="M16 9.806c-0.569 0-1.032 0.463-1.032 1.032v14.452c0 0.569 0.463 1.032 1.032 1.032s1.032-0.463 1.032-1.032v-14.452c0-0.569-0.463-1.032-1.032-1.032z" class="path3"></path>
                    <path d="M21.677 9.806c-0.569 0-1.032 0.463-1.032 1.032v14.452c0 0.569 0.463 1.032 1.032 1.032s1.032-0.463 1.032-1.032v-14.452c0-0.569-0.463-1.032-1.032-1.032z" class="path4"></path>
                </symbol>
            </defs>
        </svg>
        <!-- 用svg来替换图片的请求，性能优化END -->

        <!--主要内容-->
        <div class="wrapper">
            <div class="container">
                <div class="order-box">
                    <div class="item-address">
                        <h2 class="addr-title">收货地址</h2>
                        <div class="addr-list clearfix">
                            <div class="addr-info" :class="{'checked':index === checkIndex}" @click="checkIndex=index" v-for="(item,index) in addressList" :key="index">
                                <h2>收货人:{{item.receiverName}}</h2>
                                <div class="phone">联系电话:{{item.receiverMobile}}</div>
                                <div class="street">地址:{{item.receiverProvince + ' ' + item.receiverCity + ' ' + item.receiveDistrict + ' ' + ' ' + item.receiverAddress}}</div>
                                <div class="action">
                                    <!-- 删除地址图片 -->
                                    <a href="javascript:;" class="fl" @click="delAddress(item)" style="float: left">
                                        <svg class="icon icon-del">
                                            <!-- 使用上面定义的svg矢量图 -->
                                            <use xlink:href="#icon-del"></use>
                                        </svg>
                                    </a>
                                    <!-- 编辑地址图片 -->
                                    <a href="javascript:;" class="fr" @click="editAddressModal(item)" style="float: right">
                                        <svg class="icon icon-edit">
                                            <use xlink:href="#icon-edit"></use>
                                        </svg>
                                    </a>
                                </div>
                            </div>
                            <div class="addr-add" @click="openAddressModal">
                                <div class="icon-add"></div>
                                <div>添加新地址</div>
                            </div>
                        </div>
                    </div>

                    <div class="item-good">
                        <h3>商品</h3>
                        <ul>
                            <li v-for="item in getCheckGoods" :key="item.id">
                                <img :src="$target + item.productImg" style="float: left;width: 30px;height: 30px;margin-right: 10px;"/>
                                <div class="good-name">{{item.productName}}</div>
                                <div class="good-price">{{item.price}}元 x {{item.num}}</div>
                                <div class="good-total">{{item.price * item.num}}元</div>
                            </li>
                        </ul>
                    </div>

                    <!-- 配送方式 -->
                    <div class="item-shipping" style="border-bottom: 1px solid #E5E5E5;">
                        <h2 style="margin-bottom: 32px;font-size: 18px">配送方式</h2>
                        <span>包邮</span>
                    </div>
                    <!-- 配送方式END -->

                    <!-- 发票 -->
                    <div class="item-invoice">
                        <h2>发票</h2>
                        <a href="javascript:;">电子发票</a>
                        <a href="javascript:;">个人</a>
                    </div>
                    <!-- 发票END -->

                    <!-- 结算列表 -->
                    <div class="detail">
                        <div class="item">
                            <span class="item-name">商品件数：</span>
                            <span class="item-val">{{getCheckNum}}件</span>
                        </div>
                        <div class="item">
                            <span class="item-name">商品总价：</span>
                            <span class="item-val">{{getTotalPrice}}元</span>
                        </div>
                        <div class="item">
                            <span class="item-name">优惠活动：</span>
                            <span class="item-val">0元</span>
                        </div>
                        <div class="item">
                            <span class="item-name">运费：</span>
                            <span class="item-val">0元</span>
                        </div>
                        <div class="item-total">
                            <span class="item-name">应付总额：</span>
                            <span class="item-val" style="font-size: 24px">{{getTotalPrice}}元</span>
                        </div>
                    </div>
                    <!-- 结算列表END -->

                    <!-- 结算导航 -->
                    <div class="section-bar">
                        <div class="btn">
                            <router-link to="/shoppingCart" class="btn-base btn-return">返回购物车</router-link>
                            <a href="javascript:;" @click="addOrder" class="btn-base btn-primary">结算</a>
                        </div>
                    </div>
                    <!-- 结算导航END -->
                </div>
            </div>
        </div>
        <!--主要内容END-->
        <modal
                :title="userAction == 0? '新增地址' : '修改地址'"
                btnType="1"
                :showModal="showEditModal"
                @cancel="showEditModal=false"
                @submit="submitAddress"
        >
            <template v-slot:body>
                <div class="edit-wrap">
                    <div class="item">
                        <input type="text" class="input" placeholder="姓名" v-model="checkedItem.receiverName">
                        <input type="text" class="input" placeholder="手机号" v-model="checkedItem.receiverMobile">
                    </div>
                    <div style="margin-left: -10px"><area-select v-if="showArea" type="text" v-model="address" size="small" :data="$pcaa" :level="2" @change="changeAddress($event)"></area-select></div>
                    <div class="item">
                        <textarea name="street"  placeholder="详细地址" v-model="checkedItem.receiverAddress"></textarea>
                    </div>
                    <div class="item">
                        <input type="text" class="input" placeholder="邮编" v-model="checkedItem.receiverZip">
                    </div>
                </div>
            </template>
        </modal>

        <modal
                title="删除确认"
                btnType="1"
                :showModal="showDelModal"
                @cancel="showDelModal=false"
                @submit="submitAddress"
        >
            <template v-slot:body>
                <p>您确认要删除此地址吗？</p>
            </template>
        </modal>
    </div>
</template>

<script>
    import Modal from './../components/Modal'
    import { mapGetters } from "vuex";
    import { mapActions } from "vuex";

    import Vue from 'vue';
    import {pcaa } from 'area-data';
    import VueAreaLinkage from 'vue-area-linkage';
    import 'vue-area-linkage/dist/index.css';
    Vue.prototype.$pcaa = pcaa;
    Vue.use(VueAreaLinkage);


    export default {
        name: "ConfirmOrder",
        components: {
            Modal
        },
        data() {
            return{
                address: ['110000', '110100', '110101'],

                checkedItem: {
                    receiverProvince:'北京市',
                    receiverCity:'',
                    receiveDistrict: ''
                },
                showArea: true,
                title: '',
                addressList: '',
                userAction: '', // 用户行为: 0新增、1编辑、2删除
                showDelModal: false, // 是否显示删除弹框
                showEditModal: false, // 是否显示新增或者编辑弹框
                checkIndex: 0// 当前收货地址选中索引
            }
        },

        computed: {
            // 结算的商品数量; 结算商品总计; 结算商品信息
            ...mapGetters(["getCheckNum", "getTotalPrice", "getCheckGoods"])
        },

        mounted () {
            this.getAddressList();
        },

        methods: {
            ...mapActions(["deleteShoppingCart"]),
            addOrder() {
                this.$axios
                    .post("order/addOrder", {
                        user_id: this.$store.getters.getUser.user_id,
                        products: this.getCheckGoods
                    })
                    .then(res => {
                        let products = this.getCheckGoods;
                        switch (res.data.code) {
                            // “001”代表结算成功
                            case "001":
                                for (let i = 0; i < products.length; i++) {
                                    this.deleteShoppingCart(products[i].id);
                                }
                                // 提示结算结果
                                this.notifySucceed(res.data.msg);
                                // 跳转我的订单页面
                                this.$router.push({path: "/order"});
                                break;
                            default:
                                // 提示失败信息
                                this.notifyError(res.data.msg);
                        }
                    })
                    .catch(err => {
                        return Promise.reject(err);
                    });
            },


            openAddressModal() {
                this.userAction = 0
                this.checkedItem = {}
                this.showEditModal = true
            },
            // 打开编辑地址弹框
            editAddressModal(item) {
                this.showArea = false;
                this.userAction = 1;
                this.checkedItem = item;
                this.address[0] = item.receiverProvince;
                this.address[1] = item.receiverCity;
                this.address[2] = item.receiveDistrict;
                this.showEditModal = true
                this.$nextTick(() => {
                    this.showArea = true
                })
            },
            // 打开删除地址弹框
            delAddress(item) {
                this.checkedItem = item
                this.userAction = 2
                this.showDelModal = true
            },

            // 查询收货地址
            getAddressList() {
                this.$axios
                    .post("address/getAddressList", {
                        user_id: this.$store.getters.getUser.user_id
                    })
                    .then(res => {
                        this.addressList = res.data.addressList;
                    })
                    .catch(err => {
                        return Promise.reject(err);
                    });
            },

            // 地址删除、编辑、新增功能
            submitAddress() {
                if (this.userAction === 0 || this.userAction === 1) {
                    const {receiverName, receiverMobile, receiverAddress, receiverZip} = this.checkedItem
                    let errMsg = ''
                    if (!receiverName) {
                        errMsg = '请输入收货人名称'
                    } else if (!receiverMobile || !/\d{11}/.test(receiverMobile)) {
                        errMsg = '请输入正确格式的手机号'
                    } else if (!this.address[0]) {
                        errMsg = '请选择省份'
                    } else if (!this.address[1]) {
                        errMsg = '请选择对应的城市'
                    } else if (!this.address[2]) {
                        errMsg = '请选择对应的区'
                    } else if (!receiverAddress) {
                        errMsg = '请输入收货地址'
                    } else if (!/\d{6}/.test(receiverZip)) {
                        errMsg = '请输入六位邮编'
                    }
                    if (errMsg) {
                        this.$message.error(errMsg);
                        return
                    }
                }
                if (this.userAction == 0 || this.userAction == 1) {
                    const api = this.userAction == 0 ? 'address/addAddress' : 'address/updateAddress';
                    this.$axios
                        .post(api, {
                            user_id: this.$store.getters.getUser.user_id,
                            receiverName: this.checkedItem.receiverName,
                            receiverMobile: this.checkedItem.receiverMobile,
                            receiverProvince: this.address[0],
                            receiverCity:  this.address[1],
                            receiveDistrict: this.address[2],
                            receiverAddress: this.checkedItem.receiverAddress,
                            receiverZip: this.checkedItem.receiverZip
                        })
                        .then(res => {
                            //关闭窗口
                            this.closeModal();
                            //重新发起请求
                            this.getAddressList();
                            switch (res.data.code) {
                                case "001":
                                    this.notifySucceed(res.data.msg);
                                    break;
                                default:
                                    this.notifyError(res.data.msg);
                            }
                        })
                        .catch(err => {
                            return Promise.reject(err);
                        })
                }
                if (this.userAction == 2) {
                    this.$axios
                        .post("address/deleteAddress",{
                            user_id: this.$store.getters.getUser.user_id,
                            id: this.checkedItem.id,
                        })
                        .then(res => {
                            //关闭窗口
                            this.closeModal();
                            //重新发起请求
                            this.getAddressList();
                            switch (res.data.code) {
                                case "001":
                                    this.notifySucceed(res.data.msg);
                                    break;
                                default:
                                    this.notifyError(res.data.msg);
                            }
                        })
                        .catch(err => {
                            return Promise.reject(err);
                        })
                }
            },
            closeModal () {
                this.checkedItem = {};
                this.userAction = '';
                this.showDelModal = false;
                this.showEditModal = false
            },

            changeAddress(e) {
               this.address[0] = e[0];
               this.address[1] = e[1];
               this.address[2] = e[2];
            }
        }
    }
</script>
<style lang="scss">
    .order-confirm{
        background-color: #f5f5f5;
        .confirmOrder-header {
            background-color: #fff;
            border-bottom: 2px solid #ff6700;
            margin-bottom: 20px;
            .header-content {
                width: 1225px;
                margin: 0 auto;
                height: 80px;
                p {
                    float: left;
                    font-size: 28px;
                    line-height: 80px;
                    color: #424242;
                    margin-right: 20px;
                    i {
                        font-size: 45px;
                        color: #ff6700;
                        line-height: 80px;
                    }
                }
            }
        }
        .wrapper{
            background-color:#F5F5F5;
            padding-top:30px;
            padding-bottom:50px;
            padding-left: 150px;
            padding-right: 150px;
            height: auto;
            .order-box{
                height: auto;
                background-color:#ffffff;
                padding-left: 40px;
                padding-bottom: 40px;
                .addr-title{
                    font-size: 20px;
                    color: #333333;
                    font-weight: 200;
                    margin-bottom:21px;
                }
                .item-address{
                    height: 265px;
                    padding-top: 38px;
                    .addr-list{
                        .addr-info,.addr-add{
                            box-sizing:border-box;
                            float: left;
                            width:271px;
                            height:180px;
                            border:1px solid #E5E5E5;
                            margin-right: 15px;
                            padding: 15px 24px;
                            font-size: 14px;
                            color:#757575;
                        }
                        .addr-info{
                            cursor:pointer;
                            h2{
                                height:27px;
                                font-size:18px;
                                font-weight: 300;
                                color:#333;
                                margin-bottom:10px;
                            }
                            .street{
                                height:50px;
                            }
                            .action{
                                height:50px;
                                line-height:50px;
                                .icon{
                                    width: 20px;
                                    height: 20px;
                                    fill: #666666;
                                    vertical-align: middle;
                                    &:hover{
                                        fill: #FF6700;
                                    }
                                }
                            }
                            &.checked{
                                border:1px solid #ff6700;
                            }
                        }
                        .addr-add{
                            text-align:center;
                            color: #999999;
                            cursor:pointer;
                            .icon-add{
                                width:30px;
                                height:30px;
                                border-radius:50%;
                                background:url('https://cdn.jsdelivr.net/gh/ZTY18873242003/img/优雅的使用图床/icon-add.png') #E0E0E0 no-repeat center;
                                background-size:14px;
                                margin: 0 auto;
                                margin-top: 45px;
                                margin-bottom: 10px;
                            }
                        }
                    }
                }
                .item-good{
                    margin-top:0px;
                    border-bottom: 1px solid #E5E5E5;
                    padding-bottom: 12px;
                    h3{
                        border-bottom:1px solid #E5E5E5;
                        padding-bottom: 5px;
                    }
                    li{
                        display:flex;
                        align-items: center;
                        height:40px;
                        line-height:40px;
                        margin-top:10px;
                        font-size:16px;
                        color:#333333;
                        .good-name{
                            flex:5;
                            img{
                                width:30px;
                                height:30px;
                                vertical-align:middle;
                            }
                        }
                        .good-price{
                            flex:2;
                        }
                        .good-total{
                            padding-right:44px;
                            color:#FF6600;
                        }
                    }
                }
                .item-shipping,.item-invoice{
                    margin-top:31px;
                    line-height: 20px;
                    h2{
                        display: inline-block;
                        margin-right: 71px;
                        font-size: 20px;
                        width: 80px;
                    }
                    span,a{
                        font-size:16px;
                        color:#FF6700;
                        margin-right:23px;
                    }
                }
                .detail{
                    padding: 50px 44px 33px 0;
                    border-bottom: 1px solid #f5f5f5;
                    text-align: right;
                    font-size: 16px;
                    color: #666666;
                    .item-val{
                        color:#FF6700;
                    }
                    .item{
                        line-height: 15px;
                        margin-bottom: 12px;
                    }
                    .item-val{
                        display:inline-block;
                        width:100px;
                    }
                    .item-total{
                        .item-val{
                            font-size:28px;
                        }
                    }
                }
                .section-bar .btn {
                    float: right;
                    .btn-base {
                        float: left;
                        margin-left: 30px;
                        width: 158px;
                        height: 38px;
                        border: 1px solid #b0b0b0;
                        font-size: 14px;
                        line-height: 38px;
                        text-align: center;
                    }
                    .btn-return {
                        color: rgba(0, 0, 0, 0.27);
                        border-color: rgba(0, 0, 0, 0.27);
                    }
                    .btn-primary {
                        background: #ff6700;
                        border-color: #ff6700;
                        color: #fff;
                    }
                }
            }
        }
        .edit-wrap{
            font-size:14px;
            .item{
                margin-top: 16px;
                margin-bottom:15px;
                .input{
                    display:inline-block;
                    width:200px;
                    height:40px;
                    line-height:40px;
                    padding-left:15px;
                    border:1px solid #E5E5E5;
                    // 给.input类的兄弟元素添加样式
                    &+.input{
                        margin-left:14px;
                    }
                }
                select{
                    height:40px;
                    line-height:40px;
                    border:1px solid #E5E5E5;
                    margin-right:15px;
                }
                textarea{
                    height:62px;
                    width:100%;
                    padding:13px 15px;
                    box-sizing:border-box;
                    border:1px solid #E5E5E5;
                }
            }
        }
    }
</style>